<!--
 * @Descripttion: (采集曲线数据/cks_collected_curve_data)
 * @Author: (admin)
 * @Date: (2025-09-10)
-->
<template>
    <div>
        <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
            <el-form-item :label="$t('review.ckscollectedcurvedataPage.page.primaryCode')" prop="primaryCode">
                <el-input v-model="queryParams.primaryCode" @keyup.enter="handleQuery"
                    :placeholder="$t('review.ckscollectedcurvedataPage.page.primaryCodeInput')" clearable
                    style="width: 400px" />
            </el-form-item>

            <el-form-item :label="$t('review.ckscollectedcurvedataPage.page.createdTime')" prop="dateRange">
                <el-date-picker style="width: 360px" v-model="queryParams.dateRange" type="datetimerange"
                    :default-time="defaultTime" :shortcuts="shortcuts" value-format="YYYY-MM-DD HH:mm:ss"
                    range-separator="-" />
            </el-form-item>

            <el-form-item>
                <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
                <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
            </el-form-item>
        </el-form>
        <!-- 工具区域 -->
        <el-row :gutter="15" class="mb10">
            <el-col :span="1.5">
                <el-button type="primary" v-hasPermi="['ckscollectedcurvedata:add']" plain icon="plus"
                    @click="handleAdd">
                    {{ $t('btn.add') }}
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" :disabled="single" v-hasPermi="['ckscollectedcurvedata:edit']" plain
                    icon="edit" @click="handleUpdate">
                    {{ $t('btn.edit') }}
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" :disabled="multiple" v-hasPermi="['ckscollectedcurvedata:delete']" plain
                    icon="delete" @click="handleDelete">
                    {{ $t('btn.delete') }}
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="download" @click="handleExport"
                    v-hasPermi="['ckscollectedcurvedata:export']">
                    {{ $t('btn.export') }}
                </el-button>
            </el-col>
            <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
        </el-row>

        <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
            highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column prop="id" label="Id" width="100" align="center" v-if="columns.showColumn('id')" />
            <el-table-column prop="plcId" :label="$t('review.ckscollectedcurvedataPage.tableColumns.plcId')" width="120"
                align="center" v-if="columns.showColumn('plcId')" />
            <el-table-column prop="lineCode"
                :label="$t('review.ckscollectedcurvedataPage.tableColumns.productionLineCode')" width="150"
                align="center" v-if="columns.showColumn('lineCode')" />
            <el-table-column prop="stationCode" :label="$t('review.ckscollectedcurvedataPage.tableColumns.stationCode')"
                width="150" align="center" v-if="columns.showColumn('stationCode')" />
            <el-table-column prop="dbBlock" :label="$t('review.ckscollectedcurvedataPage.tableColumns.dbBlock')"
                width="100" align="center" v-if="columns.showColumn('dbBlock')" />
            <el-table-column prop="primaryCode" :label="$t('review.ckscollectedcurvedataPage.tableColumns.primaryCode')"
                width="280" align="center" v-if="columns.showColumn('primaryCode')" />
            <el-table-column prop="curveData" :label="$t('review.ckscollectedcurvedataPage.tableColumns.curveData')"
                width="200" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('curveData')" />
            <el-table-column prop="createTime" :label="$t('review.ckscollectedcurvedataPage.tableColumns.createTime')"
                width="240" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('createTime')" />
            <!-- <el-table-column prop="isDeleted" label="逻辑删除" align="center" v-if="columns.showColumn('isDeleted')">
                <template #default="scope">
                    <dict-tag :options="options.isDeletedOptions" :value="scope.row.isDeleted" />
                </template>
</el-table-column> -->
            <el-table-column prop="collectBatchCode"
                :label="$t('review.ckscollectedcurvedataPage.tableColumns.collectBatchCode')" width="280" align="center"
                v-if="columns.showColumn('collectBatchCode')" />
            <el-table-column :label="$t('btn.operate')" width="200" align="center">
                <template #default="scope">
                    <el-button type="success" size="small" icon="edit" v-hasPermi="['ckscollectedcurvedata:edit']"
                        @click="handleUpdate(scope.row)">
                        {{ $t("btn.edit") }}
                    </el-button>
                    <el-button type="danger" size="small" icon="delete" v-hasPermi="['ckscollectedcurvedata:delete']"
                        @click="handleDelete(scope.row)">
                        {{ $t("btn.delete") }}
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
            @pagination="getList" />


        <!-- 添加或修改采集曲线数据对话框 -->
        <el-dialog :title="title" :lock-scroll="false" v-model="open">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="150px">
                <el-row :gutter="20">

                    <!-- <el-col :lg="12" v-if="opertype != 1">
                        <el-form-item label="Id" prop="id">
                            <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入Id"
                                :disabled="true" />
                        </el-form-item>
                    </el-col> -->

                    <el-col :lg="12">
                        <el-form-item :label="$t('review.ckscollectedcurvedataPage.tableColumns.plcId')" prop="plcId">
                            <el-input v-model.number="form.plcId"
                                :placeholder="$t('review.ckscollectedcurvedataPage.formDialog.plcId')" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item :label="$t('review.ckscollectedcurvedataPage.tableColumns.dbBlock')"
                            prop="dbBlock">
                            <el-input v-model.number="form.dbBlock"
                                :placeholder="$t('review.ckscollectedcurvedataPage.formDialog.dbBlock')" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item :label="$t('review.ckscollectedcurvedataPage.tableColumns.primaryCode')"
                            prop="primaryCode">
                            <el-input v-model.number="form.primaryCode"
                                :placeholder="$t('review.ckscollectedcurvedataPage.formDialog.primaryCode')" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item :label="$t('review.ckscollectedcurvedataPage.tableColumns.createTime')"
                            prop="createTime">
                            <el-date-picker v-model="form.createTime" type="datetime" :teleported="false"
                                :placeholder="$t('review.ckscollectedcurvedataPage.formDialog.createTime')"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :lg="24">
                        <el-form-item :label="$t('review.ckscollectedcurvedataPage.tableColumns.curveData')"
                            prop="curveData">
                            <el-input type="textarea" :rows="10" v-model="form.curveData"
                                :placeholder="$t('review.ckscollectedcurvedataPage.formDialog.curveData')" />
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <template #footer v-if="opertype != 3">
                <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
                <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="ckscollectedcurvedata">
import {
    listCksCollectedCurveData,
    addCksCollectedCurveData, delCksCollectedCurveData,
    updateCksCollectedCurveData, getCksCollectedCurveData,
}
    from '@/api/business/ckscollectedcurvedata.js'
    import dayjs from 'dayjs'

const { proxy } = getCurrentInstance()
const today = dayjs()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    sort: 'id',
    sortType: 'desc',
    primaryCode: '',
    dateRange: [today.startOf('day').format('YYYY-MM-DD HH:mm:ss'), today.endOf('day').format('YYYY-MM-DD HH:mm:ss')],
})
const columns = ref([
    { visible: true, prop: 'id', label: 'Id' },
    { visible: true, prop: 'plcId', label: 'PLCID' },
    { visible: true, prop: 'lineCode', label: '产线' },
    { visible: true, prop: 'stationCode', label: '工位' },
    { visible: true, prop: 'dbBlock', label: '数据块' },
    { visible: true, prop: 'primaryCode', label: '主产品码' },
    { visible: true, prop: 'curveData', label: '曲线数据JSON' },
    { visible: true, prop: 'createTime', label: '创建时间' },
    { visible: true, prop: 'collectBatchCode', label: '采集批次' },
    { visible: true, prop: 'isDeleted', label: '逻辑删除' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
// 时间范围快捷选项
const shortcuts = [
  {
    text: proxy.$t('common.today'),
    value: () => {
      const start = new Date().setHours(0, 0, 0, 0)
      const end = new Date().setHours(23, 59, 59, 999)
      return [start, end]
    },
  },
  {
    text: proxy.$t('common.thisWeek'),
    value: () => {
      const start = new Date()
      const end = new Date().setHours(23, 59, 59, 999)
      start.setHours(0, 0, 0, 0)
      start.setDate(start.getDate() - 7)
      return [start, end]
    },
  },
  {
    text: proxy.$t('common.thisMonth'),
    value: () => {
      const start = new Date()
      const end = new Date().setHours(23, 59, 59, 999)
      start.setHours(0, 0, 0, 0)
      start.setMonth(start.getMonth() - 1)
      return [start, end]
    },
  },
  {
    text: proxy.$t('common.last3Month'),
    value: () => {
      const start = new Date()
      const end = new Date().setHours(23, 59, 59, 999)
      start.setHours(0, 0, 0, 0)
      start.setMonth(start.getMonth() - 3)
      return [start, end]
    },
  },
]

var dictParams = [
]


function getList() {
    loading.value = true
    const formBody = JSON.parse(JSON.stringify(queryParams));
    proxy.addDateRange(formBody, queryParams.dateRange);
    delete formBody.dateRange;
    listCksCollectedCurveData(formBody).then(res => {
        const { code, data } = res
        if (code == 200) {
            dataList.value = data.result
            total.value = data.totalNum
            loading.value = false
        }
    })
}

// 查询
function handleQuery() {
    queryParams.pageNum = 1
    getList()
}

// 重置查询操作
function resetQuery() {
    proxy.resetForm("queryRef")
    handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1
    multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
    var sort = undefined
    var sortType = undefined

    if (column.prop != null && column.order != null) {
        sort = column.prop
        sortType = column.order

    }
    queryParams.sort = sort
    queryParams.sortType = sortType
    handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
    single: true,
    multiple: true,
    form: {},
    rules: {
        plcId: [{ required: true, message: proxy.$t('review.ckscollectedcurvedataPage.formDialogVerify.plcId'), trigger: "blur", type: "number" }],
        dbBlock: [{ required: true, message: proxy.$t('review.ckscollectedcurvedataPage.formDialogVerify.dbBlock'), trigger: "blur", type: "number" }],
        primaryCode: [{ required: true, message: proxy.$t('review.ckscollectedcurvedataPage.formDialogVerify.primaryCode'), trigger: "blur", type: "string" }],
        curveData: [{ required: true, message: proxy.$t('review.ckscollectedcurvedataPage.formDialogVerify.curveData'), trigger: "blur" }],
    },
    options: {
        // 逻辑删除 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
        isDeletedOptions: [],
    }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
    open.value = false
    reset()
}

// 重置表单
function reset() {
    form.value = {
        id: null,
        plcId: null,
        dbBlock: null,
        curveData: null,
        createTime: null,
        isDeleted: null,
    };
    proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
    reset();
    open.value = true
    title.value =  proxy.$t('review.ckscollectedcurvedataPage.formDialog.addTitle')
    opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
    reset()
    const id = row.id || ids.value
    getCksCollectedCurveData(id).then((res) => {
        const { code, data } = res
        if (code == 200) {
            open.value = true
            title.value = proxy.$t('review.ckscollectedcurvedataPage.formDialog.editTitle')
            opertype.value = 2

            form.value = {
                ...data,
            }
        }
    })
}

// 添加&修改 表单提交
function submitForm() {
    proxy.$refs["formRef"].validate((valid) => {
        if (valid) {

            if (form.value.id != undefined && opertype.value === 2) {
                updateCksCollectedCurveData(form.value).then((res) => {
                    proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
                    open.value = false
                    getList()
                })
            } else {
                addCksCollectedCurveData(form.value).then((res) => {
                    proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
                    open.value = false
                    getList()
                })
            }
        }
    })
}

// 删除按钮操作
function handleDelete(row) {
    const Ids = row.id || ids.value

    proxy
        .$confirm(proxy.$t("common.deleteConfirm", [Ids]), proxy.$t("common.systemTips"), {
            confirmButtonText: proxy.$t('common.ok'),
            cancelButtonText: proxy.$t('common.cancel'),
            type: "warning",
        })
        .then(function () {
            return delCksCollectedCurveData(Ids)
        })
        .then(() => {
            getList()
            proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
        })
}

// 导出按钮操作
function handleExport() {
    const formBody = JSON.parse(JSON.stringify(queryParams));
    proxy.addDateRange(formBody, queryParams.dateRange);
    delete formBody.dateRange;
    proxy
        .$confirm(proxy.$t("common.exportConfirm"), proxy.$t("common.systemTips"), {
            confirmButtonText: proxy.$t('common.ok'),
            cancelButtonText: proxy.$t('common.cancel'),
            type: "warning",
        })
    .then(async () => {
      await proxy.downFile('/business/CksCollectedCurveData/export', formBody)
    })
}


handleQuery()
</script>